package com.example.hao.ximalaya.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.hao.ximalaya.R;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    @BindView(R.id.stl)
    SmartTabLayout stl;
    @BindView(R.id.vp)
    ViewPager vp;
    Unbinder unbinder;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initView();
        vp.setCurrentItem(1);


        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void initView() {

        vp.setAdapter(new PagerAdapter(getChildFragmentManager()));
        stl.setViewPager(vp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    class PagerAdapter extends FragmentPagerAdapter {


        private PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void setPageTitle(String[] pageTitle) {
            this.pageTitle = pageTitle;
        }

        String[] pageTitle = {"分类", "推荐", "VIP", "直播", "小说", "广播", "儿童", "精品", "段子", "音乐", "相声", "历史", "人文", "情感", "英语"};
        Fragment[] fragments = new Fragment[pageTitle.length];


        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return pageTitle[position];
        }

        @Override
        public Fragment getItem(int position) {
            BlankFragment blankFragment = new BlankFragment();
            if (fragments[position] == null) {
                switch (position) {
                    case 1:
                        return fragments[position] = new RecommendFragment();
                    default:
                        Bundle bundle = new Bundle();
                        bundle.putString("title", pageTitle[position]);
                        blankFragment.setArguments(bundle);
                        return fragments[position] = blankFragment;
                }

            }
            return null;
        }

        @Override
        public int getCount() {

            return fragments == null ? 0 : fragments.length;
        }


    }

}
